package com.eyaomall.wxmanage.common.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.beanutils.BeanUtils;

import com.eyaomall.wxmanage.common.util.EhCacheUtil;
import com.eyaomall.wxmanage.model.po.Area;
import com.lycheeframework.core.common.helper.GlobEnv;
import com.lycheeframework.core.common.util.ListUtil;
import com.lycheeframework.core.common.util.MapUtil;
/**
 * 地区资源帮助类
 * @author pro
 *
 */
public class AreaHelper {
	
	/**
	 * 根据ID获取区域名称
	 * @param id
	 * @return
	 */
	public static String getAreaName(int id){
		Area area = getAreaByID(id);
		if(area == null) return null;
		
		return area.getAreaName();
	}
	
	/**
	 * 根据ID返回区域对象
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Area getAreaByID(int id) {
		Map<Integer, Area> areasMap = null;
		
		if ("n".equalsIgnoreCase(GlobEnv.get("ocs.locked"))) {
			areasMap = new HashMap<Integer, Area>();
			Map<Integer, HashMap<String, Object>> am = (Map<Integer, HashMap<String, Object>>) OcsHelper.get("areasMap");
			
			try {
				for (Entry<Integer, HashMap<String, Object>> entry : am.entrySet()) {
					Area area = new Area();
					BeanUtils.populate(area, entry.getValue());
					areasMap.put(entry.getKey(), area);
				}
			} catch (Exception e) {
				areasMap = null;
			}
		}
		
		if (MapUtil.isEmpty(areasMap)) 
			areasMap = (Map<Integer, Area>) EhCacheUtil.get("areasMap");
		
		if (MapUtil.isEmpty(areasMap)) return null;
		
		return areasMap.get(id);
	}
	
	/**
	 * 返回所有城市列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Area> getCitys() {
		List<Area> citys = null;
		
		if ("n".equalsIgnoreCase(GlobEnv.get("ocs.locked"))) 
			citys = getAreas("area.citys");
		
		if (ListUtil.isEmpty(citys)) 
			citys = (List<Area>) EhCacheUtil.get("area.citys");
		
		return citys;
	}
	
	/**
	 * 返回所有区域列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Area> getCountys() {
		List<Area> countys = null;
		
		if ("n".equalsIgnoreCase(GlobEnv.get("ocs.locked"))) 
			countys = getAreas("area.countys");
		
		if (ListUtil.isEmpty(countys)) 
			countys = (List<Area>) EhCacheUtil.get("area.countys");
		
		return countys;
	}
	
	/**
	 * 根据ParentID返回区域列表
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Area> getAreaByParentID(int id) {
		Map<Integer, List<Area>> areasMap = null;
		
		if ("n".equalsIgnoreCase(GlobEnv.get("ocs.locked"))) {
			
			areasMap = new HashMap<Integer, List<Area>>();
			Map<Integer, List<HashMap<String, Object>>> am = (Map<Integer, List<HashMap<String, Object>>>) OcsHelper.get("areaByID");
			
			try {
				for (Entry<Integer, List<HashMap<String, Object>>> entry : am.entrySet()) {
					List<Area> areas = new ArrayList<Area>();
					for (HashMap<String, Object> hm: entry.getValue()) {
						Area area = new Area();
						BeanUtils.populate(area, hm);
						areas.add(area);
					}
					
					areasMap.put(entry.getKey(), areas);
				}
			} catch (Exception e) {
				areasMap = null;
			}
		}
		
		if (MapUtil.isEmpty(areasMap)) 
			areasMap = (Map<Integer, List<Area>>) EhCacheUtil.get("areaByID");
		
		if (MapUtil.isEmpty(areasMap)) return null;
		
		return areasMap.get(id);
	}
	
	/**
	 * 根据cityId返回地区以及商圈信息
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> getCountySubDistrictByCityId(int cityId) {
		Map<Integer, List<Map<String, Object>>> cityMap = null;
		
		if ("n".equalsIgnoreCase(GlobEnv.get("ocs.locked"))) 
			cityMap = (Map<Integer, List<Map<String, Object>>>) OcsHelper.get("countySubDistrictByCityIdMap");
		
		if (MapUtil.isEmpty(cityMap)) 
			cityMap =(Map<Integer, List<Map<String, Object>>>) EhCacheUtil.get("countySubDistrictByCityIdMap");
		
		if (MapUtil.isEmpty(cityMap)) return null;
		
		return cityMap.get(cityId);
	}
	
	/**
	 * 返回区域级联列表
	 * @return
	 */
	public static List<?> getCascadeArea() {
		List<?> cascadeArea = null;
		
		if ("n".equalsIgnoreCase(GlobEnv.get("ocs.locked"))) 
			cascadeArea = (List<?>) OcsHelper.get("cascadeArea");
		
		if (ListUtil.isEmpty(cascadeArea)) 
			cascadeArea = getAreas("cascadeArea");
		
		return cascadeArea;
	}
	
	@SuppressWarnings("unchecked")
	private static List<Area> getAreas(String key) {
		List<HashMap<String, Object>> arrays = (List<HashMap<String, Object>>)OcsHelper.get(key);
		
		List<Area> areas = new ArrayList<Area>();
		try {
			for (int i = 0; i < arrays.size(); i++) {
				Area area = new Area();
				BeanUtils.populate(area, arrays.get(i));
				areas.add(area);
			}
		} catch (Exception e) {
			
			return null;
		}
		
		return areas;
	}

	/**
	 * 返回所有省列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Area> getProvinces() {
		List<Area> provinces = null;
		
		if ("n".equalsIgnoreCase(GlobEnv.get("ocs.locked"))) 
			provinces = (List<Area>) OcsHelper.get("area.provinces");
		
		if (ListUtil.isEmpty(provinces)) 
			provinces = (List<Area>) EhCacheUtil.get("area.provinces");
		
		return provinces;
	}
	
	/**
	 * 获取所有区域数据
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Area> listCountys(){
		
		return (List<Area>) EhCacheUtil.get("area.countys");
	}
}